﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace Genuilder.Extensibility
{
	//public interface CodeItemFactory
	//{
	//    CodeItem Create(string name);
	//}

	//public interface CodeItemHistoryRepository
	//{
	//    CodeItemHistory GetHistory(string itemName);
	//    CodeItemHistory[] Histories
	//    {
	//        get;
	//    }
	//}

	//public class GenSession
	//{

	//    CodeItemFactory _Factory;
	//    ObservableCollection<CodeItem> _CodeItems = new ObservableCollection<CodeItem>();
	//    ReadOnlyObservableCollection<CodeItem> _CodeItemsRO;

	//    public GenSession(CodeItemFactory itemFactory)
	//    {
	//        _Factory = itemFactory;
	//        _CodeItems.CollectionChanged += new NotifyCollectionChangedEventHandler(_CodeItems_CollectionChanged);
	//    }

	//    void _CodeItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
	//    {
	//        if(e.NewItems != null)
	//            foreach(CodeItem item in e.NewItems)
	//                OnCodeItemAdded(item);

	//    }

	//    private void OnCodeItemAdded(CodeItem item)
	//    {
	//        if(CodeItemAdded != null)
	//            CodeItemAdded(item);
	//    }

	//    public IList<CodeItem> CodeItems
	//    {
	//        get
	//        {
	//            if(_CodeItemsRO == null)
	//                _CodeItemsRO = new ReadOnlyObservableCollection<CodeItem>(_CodeItems);
	//            return _CodeItemsRO;
	//        }
	//    }

	//    public event Action<CodeItem> CodeItemAdded;

	//    public CodeItem Get(string name)
	//    {
	//        var item = CodeItems.FirstOrDefault(i => i.Name == name);
	//        if(item == null)
	//        {
	//            item = _Factory.Create(name);
	//            _CodeItems.Add(item);
	//        }
	//        return item;
	//    }

	//    public CodeDependency Get(string name, CodeItem source)
	//    {
	//        var target = Get(name);
	//        return new CodeDependency(source, target);
	//    }

	//    internal void RunSession(IEnumerable<string> sourceItems)
	//    {
	//        if(sourceItems.Count() == 0)
	//        {
	//            var notPlayedHistories = histories.Histories;
	//            return;
	//        }
	//        List<string> newItems = new List<string>();
	//        NotifyCollectionChangedEventHandler onCodeItemsChanged = (s, a) =>
	//        {
	//            if(a.NewItems != null)
	//                foreach(CodeItem item in a.NewItems)
	//                    newItems.Add(item.Name);
	//            else if(a.OldItems != null)
	//                foreach(CodeItem item in a.OldItems)
	//                    newItems.Remove(item.Name);
	//            else
	//                throw new NotSupportedException();
	//        };
	//        try
	//        {
	//            _CodeItems.CollectionChanged += onCodeItemsChanged;
	//            foreach(var item in sourceItems)
	//            {
	//                var codeItem = Get(item);
	//                var history = histories.GetHistory(item);

	//                if(history != null)
	//                    history.PlayChange(codeItem);
	//            }
	//        }
	//        finally
	//        {
	//            _CodeItems.CollectionChanged -= onCodeItemsChanged;
	//        }
	//        RunSession(newItems, histories);
	//    }
	//}
}
